<template>
  <div>
    <el-dialog
      title="添加抽奖赠卷"
      :close-on-click-modal="false"
      append-to-body
      :visible.sync="visible"
      width="80%">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.key" placeholder="优惠券名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button type="danger" @click="dataFormSubmit()" :disabled="dataListSelections.length <= 0">确认选择赠卷</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="id"
          header-align="center"
          align="center"
          label="ID">
        </el-table-column>
        <el-table-column
          prop="code"
          header-align="center"
          align="center"
          label="优惠券编号">
        </el-table-column>
        <el-table-column
          prop="title"
          header-align="center"
          align="center"
          label="优惠券名称">
        </el-table-column>
        <el-table-column
          prop="releaseType"
          header-align="center"
          align="center"
          label="优惠券类型">
          <template slot-scope="scope">
            <el-tag type="success">{{scope.row.releaseType === 1 ? '平台赠券' : (scope.row.releaseType === 2 ? '消费赠券' : '签到赠券')}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="couponStandard"
          header-align="center"
          align="center"
          label="赠券标准">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.relaeaseType === 1">平台赠券</el-tag>
            <el-tag type="warning" v-if="scope.row.relaeaseType === 2">消费{{scope.row.couponStandard}}赠券</el-tag>
            <el-tag type="warning" v-if="scope.row.relaeaseType === 3">签到{{scope.row.couponStandard}}赠券</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="discountType"
          header-align="center"
          align="center"
          label="优惠类型">
          <template slot-scope="scope">
            <el-tag type="info">{{scope.row.discountType === 1 ? '金额优惠' : '折扣优惠'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          label="启用标识">
          <template slot-scope="scope">
            <el-switch
              style="margin-left: 10px;margin-bottom: 5px;"
              v-model="scope.row.state === 1"
              @change="batch(scope.row.id,scope.row.state)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="circulation"
          header-align="center"
          align="center"
          label="发行量">
        </el-table-column>
        <el-table-column
          prop="stock"
          header-align="center"
          align="center"
          label="剩余库存">
        </el-table-column>
        <el-table-column
          prop="remarks"
          header-align="center"
          align="center"
          label="备注信息">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="userCouponHandle(scope.row.id)">领取记录</el-button>
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">查看</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <user-coupon v-if="addOrUpdateVisible1" ref="userCoupon" @refreshDataList="getDataList"></user-coupon>
    </el-dialog>
  </div>
</template>
<script>
  import AddOrUpdate from '../shoppingcoupon/shoppingcoupon-add-or-update'
  import UserCoupon from '../usercoupon/shoppingusercoupon'
  export default {
    data () {
      return {
        luckDrawIndex: null, // 始标识为本模块做用域
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        visible: false,
        idsGoods: [],
        subGoodsInfo: [],
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addOrUpdateVisible1: false
      }
    },
    components: {
      AddOrUpdate,
      UserCoupon
    },
    methods: {
      init () {
        debugger
        /* if (idList.length !== 0 && infoList.length !== 0) {
          this.idsGoods = idList
          this.subGoodsInfo = infoList
        } */
        this.idsGoods = []
        this.subGoodsInfo = []
        this.getDataList()
        console.log(this.dataListSelections)
      },
      // 获取数据列表
      getDataList () {
        debugger
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/shopping/coupon/queryCouponList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.key,
            'luckDrawIndex': this.luckDrawIndex
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.visible = true
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      userCouponHandle (id) {
        this.addOrUpdateVisible1 = true
        this.$nextTick(() => {
          this.$refs.userCoupon.init(id)
        })
      },
      dataFormSubmit () {
        if (this.dataListSelections.length === 0) {
          return this.$message.error('请选择商品')
        } else if (this.dataListSelections.length > 1) {
          return this.$message.error('只能选择一个商品')
        } else {
          this.idsGoods.push(this.dataListSelections[0].id)
          this.subGoodsInfo.push({
            'id': this.dataListSelections[0].id,
            'code': this.dataListSelections[0].code,
            'title': this.dataListSelections[0].title,
            'releaseType': this.dataListSelections[0].releaseType,
            'stock': this.dataListSelections[0].stock
          })
          this.visible = false
          this.dataListLoading = true
          this.$emit('refsGetGoodsList', this.idsGoods, this.subGoodsInfo)
        }
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/shopping/coupon/removeCouponByIds'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      batch (id, state) {
        this.$http({
          url: this.$http.adornUrl(`/shopping/coupon/modifyCouponStateById/${id}/${state}`),
          method: 'post',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
